<?php
namespace app\authorization\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use tree\Tree;
use app\admin\model\AdminMenuModel;
use app\admin\model\AccessModel;
use app\authorization\model\RbacMenuModel as RMM;
/**
 * 20200415
 * 权限菜单管理
 */
class RbacMenuController extends AdminBaseController
{	
	private $rbacMenuModel;
	public function _initialize()
	{
		$this->rbacMenuModel = new RMM();
	}
	/**
	 * 权限菜单首页
	 */
	public function index ()
	{	
		$roleList = $this->rbacMenuModel->getAllRoleInfo( 0 , function( $id , $count ){
			$str = '&nbsp;&nbsp;&nbsp;&nbsp;<i class="layui-icon layui-icon-user btn-user-count" style="color: #3A8DFE;font-size:16px;" title="角色成员">'.$count.'</i>';
			if( $id != 1 ){
				$str .= '&nbsp;&nbsp;<i class="layui-icon layui-icon-auz btn-quanxian " style="color: #3A8DFE;font-size:16px;" title="权限菜单设置"></i>';
			}
			return $str;
		} );
		$this->assign( 'list' , json_encode( $roleList , JSON_UNESCAPED_UNICODE ) );
		return $this->fetch();
	}
	/**
	 *设置权限
	 */
	public function setRoleAuth()
	{
		$param = $this->request->param();
		$roleId = $param [ 'roleId' ];
		$list = $this->rbacMenuModel->getMenuListByRoleId( $roleId );
		if( isset( $list [ 'status' ] ) && $list [ 'status' ] === false  ){
			echo $list['message'];
			$list = [];
		}
		$this->assign( 'list' , json_encode( $list , JSON_UNESCAPED_UNICODE ) );
		return $this->fetch( 'rbac_menu/set_auth' );
	}	
	/**
	 * 设置权限保存
	 */
	public function setRoleAuthPost()
	{
		if( !$this->request->isPost() ){
			return zy_json_echo( false , '请求类型错误!' );
		}
		$param = $this->request->param();
		$menuList = isset( $param [ 'list' ] ) ? $param [ 'list' ] : [] ; 
		$roleId = $param [ 'roleId' ];
		$res = $this->rbacMenuModel->saveRoleMenu( $roleId , $menuList );
		if( !$res ){
			return zy_json_echo( false , '授权信息保存失败,请稍后再试!' );
		}
		return zy_json_echo( true, '授权信息保存操作成功! '.( empty( $menuList ) ? '已清除角色的所有权限!' : '' )    );
	}

	/**
	 * 查看用户
	 */
	public function userList()
	{
		$param = $this->request->param();
		$roleId = $param [ 'roleId' ];
		$user = $this->rbacMenuModel->getUserByRoleId( $roleId );
		$this->assign( 'user' , $user );
		return $this->fetch( 'rbac_menu/user_list' );
	}


	public function dd($id)
	{
		$res = $this->rbacMenuModel->getRbacListByUserId( $id );
		dump( $res );
	}

}